package cn.wolfcode.web.controller;

import cn.wolfcode.transfer.domain.Business;
import cn.wolfcode.transfer.domain.BusinessConsumption;
import cn.wolfcode.transfer.qo.BusinessConsumptionQueryObject;
import cn.wolfcode.transfer.service.IBusinessReportService;
import cn.wolfcode.transfer.service.IBusinessService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/businessReport")
@Controller
public class BusinessReportController {
    @Autowired
    private IBusinessReportService businessReportService;
    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, BusinessConsumptionQueryObject qo) {
        PageInfo<BusinessConsumption> select = businessReportService.select(qo);
        List<Business> businesses = businessService.listAll();
        model.addAttribute("pageInfo", select);
        model.addAttribute("businesses", businesses);
        model.addAttribute("qo", qo);

        return "/businessReport/list";
    }

    @RequestMapping("/histogram")
    public String histogram(Model model, BusinessConsumptionQueryObject qo) {
        qo.setPageSize(0);
        PageInfo<BusinessConsumption> select = businessReportService.select(qo);
        ArrayList<Object> title = new ArrayList<>();
        ArrayList<Object> totalAmount = new ArrayList<>();
        ArrayList<Object> payAmount = new ArrayList<>();
        ArrayList<Object> discountAmount = new ArrayList<>();
        ArrayList<Object> count = new ArrayList<>();
        for (BusinessConsumption businesse : select.getList()) {
            title.add(businesse.getTitle());
            totalAmount.add(businesse.getTotalAmount());
            payAmount.add(businesse.getPayAmount());
            discountAmount.add(businesse.getDiscountAmount());
            count.add(businesse.getCount());
        }
        model.addAttribute("title", JSON.toJSONString(title));
        model.addAttribute("totalAmount", JSON.toJSONString(totalAmount));
        model.addAttribute("payAmount", JSON.toJSONString(payAmount));
        model.addAttribute("discountAmount", JSON.toJSONString(discountAmount));
        model.addAttribute("count", JSON.toJSONString(count));

        return "/businessReport/histogram";
    }
}
